import settings from '@/settings'
import request from '@utils/request'
import { APIResult } from '@utils/request/type'
import { VariableDTO } from '@/api/entity/variable'
import { GatewayAndDeviceStatusVO } from '@/api/entity/realtime'
import { getToken } from '@utils/authorization/token'
import { ServerEnum } from '@/api/enum/server'
import Authorization from '@utils/authorization'
import { WebSocketPlus } from '@utils/websocket/WebSocketPlus'

/**
 * 获取实时数据WebSocket实例
 * @param params
 */
export const getRealDataWebSocketAPI = (params: { deviceId: string }) => {
  return new WebSocketPlus(settings.URL.WEBSOCKET_URL + ServerEnum.DataGatherer + `/websocket/${getToken()}/${params.deviceId}`)
}

/**
 * 获取一次实时数据
 * @param params
 */
export const getRealDataAPI = (params: {
  // 设备id
  tableName: string
}) => request.post<APIResult<{
  // 数据时间
  ts: string,
  // Record<变量地址, 变量值>
  lastData: Record<string, string>
}>>(ServerEnum.DataGatherer + '/dataOperation/getLastData', params)

/**
 * 获取多个设备的实时数据
 * @param params
 * @return Record<设备Id, {...}>
 */
export const getRealDataByIdsAPI = (params: {
  // 设备id
  deviceIds: string[]
}) => request.post<APIResult<Record<string, {
  // 数据时间
  ts: string,
  // 设备Id
  deviceId: string,
  variableData: VariableDTO[]
}>>>(ServerEnum.DataGatherer + '/dataOperation/getLastDataByDeviceIds', params)

/**
 * 获取设备和网关在离线状态
 */
export const getDeviceAndGatewayStatusAPI = () => request.post<APIResult<GatewayAndDeviceStatusVO>>(ServerEnum.System + '/index/gatewayAndDeviceStatus')
